import { defineStore } from 'pinia';
import { store } from '@/store';
import { getAuthCache, setAuthCache } from '@/utils/auth';
import { DICTIONARY_KEY } from '@/enums/cacheEnum';
interface FormState {
  layerFormInfo: any;
  layerQuestions: Array<any>;
  formInfo: any;
  questions: Array<any>;
  dictionary: any;
}

export const formInfoStore = defineStore({
  id: 'app-form-info',
  state: (): FormState => ({
    layerFormInfo: {}, //弹层表单信息
    layerQuestions: [], //弹层表单配置
    formInfo: {}, //表单信息
    questions: [], //表单配置
    dictionary: getAuthCache(DICTIONARY_KEY) //接口数据字典
  }),
  getters: {
    getFormInfo(): any {
      return this.formInfo || {};
    },
    getQuestions(): any {
      return this.questions || [];
    },
    getLayerFormInfo(): any {
      return this.layerFormInfo || {};
    },
    getLayerQuestions(): any {
      return this.layerQuestions || [];
    },
    getDictionary(): any {
      return this.dictionary || {};
    }
  },
  actions: {
    setDictionary(data: any) {
      if (!this.dictionary) {
        this.dictionary = {};
      }
      if (data) {
        this.dictionary = Object.assign(this.dictionary, data);
        setAuthCache(DICTIONARY_KEY, this.dictionary);
      }
    },
    setFormInfo(info: string | undefined) {
      this.formInfo = info ? info : {}; 
    },
    setLayerFormInfo(info: string | undefined) {
      this.layerFormInfo = info ? info : {};
    },
    setQuestions(questions: any, data: any = null) {
      if (data && questions && questions.length) {
        questions.forEach((question: any) => {
          for (const key in data) {
            if (question.key.includes('.')) {
              const keys: any = question.key.split('.');
              if (data[keys[0]]) {
                question.value = keys.reduce((acc, curr) => acc[curr], data);
              } else {
                question.value = '';
              }
            } else if (key == question.key) {
              question.value = data[key];
            }
          }
        });
      }
      this.questions = questions;
    },
    setLayerQuestions(questions: any, data: any = null) {
      if (data && questions && questions.length) {
        questions.forEach((question: any) => {
          for (const key in data) {
            if (question.key.includes('.')) {
              const keys: any = question.key.split('.');
              if (data[keys[0]]) {
                question.value = keys.reduce((acc, curr) => acc[curr], data);
              } else {
                question.value = '';
              }
            } else if (key == question.key) {
              question.value = data[key];
            }
          }
        });
      }
      this.layerQuestions = questions;
    },
    clearFormInfo() {
      this.formInfo = {};
    },
    clearLayerFormInfo() {
      this.layerFormInfo = {};
    },
    //更新questions里面组件状态
    updateQuestions(changeOptions: any) {
      changeOptions &&
        changeOptions.forEach((item: any) => {
          if (this.questions && this.questions.length) {
            this.questions.forEach((question: any, index: number) => {
              if (item.key === question.key) {
                if (item.option.includes('.')) {
                  eval(`question.${item.option} = ${JSON.stringify(item.value)}`);
                } else {
                  question[item.option] = item.value;
                }
              }
            });
          }
        });
    },
    //更新questions里面组件状态
    updateLayerQuestions(changeOptions: any) {
      console.log('strot', changeOptions);
      changeOptions &&
        changeOptions.forEach((item: any) => {
          if (this.layerQuestions && this.layerQuestions.length) {
            this.layerQuestions.forEach((question: any, index: number) => {
              if (item.key === question.key) {
                if (item.option.includes('.')) {
                  eval(`question.${item.option} = ${JSON.stringify(item.value)}`);
                } else {
                  question[item.option] = item.value;
                }
              }
            });
          }
        });
    },
    //删除指定key的Questions
    deleteQuestions(questions) {
      if (questions.key) {
        for (let i = this.questions.length - 1; i >= 0; i--) {
          if (this.questions[i].key === questions.key) {
            this.questions.splice(i, 1);
          }
        }
      }
    },
    deleteLayerQuestions(questions) {
      if (questions.key) {
        for (let i = this.layerQuestions.length - 1; i >= 0; i--) {
          if (this.layerQuestions[i].key === questions.key) {
            this.layerQuestions.splice(i, 1);
          }
        }
      }
    }
  }
});

//需要在设置之外使用
export function formStoreWithOut() {
  return formInfoStore(store);
}

//使用例子
//获取
//const formInfo = computed(() => formStore.getFormInfo);
//const questions = computed(() => formStore.getQuestions);
//设置
//import { formStoreWithOut } from '@/store/modules/user';
//const formStore = formStoreWithOut();
//formStore.setFormInfo({});
//formStore.setQuestions({});
